﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using quanlynhasach.Linksql;

namespace quanlynhasach
{
    public partial class frmTheloai : Form
    {
        Connection db = new Connection();

        public frmTheloai()
        {
            InitializeComponent();
        }

        private void frmTheloai_Load(object sender, EventArgs e)
        {
            this.Dock = DockStyle.Fill;
            dataGridView1.DefaultCellStyle.ForeColor = Color.Blue;
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.DataSource = db.getDS("Select * from TheLoai where MaTheLoai <> 'N/A'");
        }

        private void Enable(bool a)
        {
            dataGridView1.Enabled = !a;
            them.Enabled = !a;
            sua.Enabled = !a;
            xoa.Enabled = !a;
            luu.Enabled = a;
            huy.Enabled = a;
            ma.Enabled = a;
            ten.Enabled = a;
        }

        public bool XoaTL(string MaTL)
        {
            string[] param = { "@MaTheLoai" };
            object[] value = { MaTL };
            string query = "Delete from TheLoai where MaTheLoai=@MaTheLoai";
            return db.ExecuteNonQueryPara(query, param, value);
        }

        public bool SuaTL(string ma, string hoten)
        {
            string[] param = { "@MaTheLoai", "@TenTL" };
            object[] value = { ma, hoten };
            string query = "Update TheLoai set TenTL=@TenTL where MaTheLoai=@MaTheLoai";
            return db.ExecuteNonQueryPara(query, param, value);
        }

        public bool ThemTL(string ma, string hoten)
        {
            string[] param = { "@MaTheLoai", "@TenTL" };
            object[] value = { ma, hoten };
            string query = "Insert into TheLoai (MaTheLoai, TenTL) values (@MaTheLoai, @TenTL)";
            return db.ExecuteNonQueryPara(query, param, value);
        }

        private void them_Click(object sender, EventArgs e)
        {
            Enable(true);
            ma.Text = "";
            ten.Text = "";
        }

        private void sua_Click(object sender, EventArgs e)
        {
            Enable(true);
        }

        private void xoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc là xóa thể loại " + ma.Text + " này không ?. Nếu xóa, tất cả dữ liệu liên quan sẽ bị xóa theo.", "Xác nhận", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)

                if (XoaTL(ma.Text))
                    MessageBox.Show("Xóa thành công !");
            dataGridView1.DataSource = db.getDS("Select * from TheLoai where MaTheLoai <> 'N/A'");
        }

        private void luu_Click(object sender, EventArgs e)
        {
            if (db.getDS("Select * from TheLoai where MaTheLoai = '" + ma.Text + "'").Rows.Count > 0)
                if (SuaTL(ma.Text, ten.Text))
                    MessageBox.Show("Cập nhật thành công");
                else
                    MessageBox.Show("Xem lại !");
            else
                if (ThemTL(ma.Text, ten.Text))
                    MessageBox.Show("Thêm thành công !");
                else
                    MessageBox.Show("Xem lại !");
            dataGridView1.DataSource = db.getDS("Select * from TheLoai where MaTheLoai <> 'N/A'");
            Enable(false);
        }

        private void huy_Click(object sender, EventArgs e)
        {
            Enable(false);
            dataGridView1.DataSource = db.getDS("Select * from TheLoai where MaTheLoai <> 'N/A'");
        }

        private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            int Rs = e.RowIndex;
            ma.Text = dataGridView1.Rows[Rs].Cells["MaTheLoai"].Value.ToString();
            ten.Text = dataGridView1.Rows[Rs].Cells["TenTL"].Value.ToString();
        }


    }
}
